/**
 * 
 */
package com.lanswon.qzsmk.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Base64;

/**
 * @author weizh
 *
 */
public class Password {
	private String password;

	  public static Password encrypt(String pass) throws NoSuchAlgorithmException  {
	    if (pass == null)
	      pass = "";
	    return new Password(encryptString(pass));
	  }

	  private static String encryptString(String pass) throws NoSuchAlgorithmException
	  {
	    return encode(digest(pass));
	  }

	  public Password(String string)
	  {
	    this.password = string;
	  }

	  public String getPassword()
	  {
	    return this.password;
	  }

	  public void setPassword(String string)
	  {
	    this.password = string;
	  }

	  private static byte[] digest(String str) throws NoSuchAlgorithmException {
	    MessageDigest dig = MessageDigest.getInstance("MD5");
	    dig.update(str.getBytes());
	    return dig.digest();
	  }

	  private static String encode(byte[] bytes) {
		    
	    Base64 encoder = new Base64();
	    return encoder.encodeToString(bytes);
	  }

	  public boolean verify(String string)
	    throws NoSuchAlgorithmException
	  {
	    return this.password.equals(encryptString(string));
	  }

	  public String toString() {
	    return this.password;
	  }

	  public boolean equals(Object obj) {
	    if (this == obj)
	      return true;
	    if ((obj == null) || (!(obj instanceof Password)))
	      return false;
	    Password pass = (Password)obj;
	    return this.password.equals(pass.password);
	  }

	  public int hashCode() {
	    return this.password.hashCode();
	  }

	  public static void main(String[] arg0) throws Exception {
		 long b = System.currentTimeMillis();
	    Password p = encrypt("再次");
	    
	    System.out.println(p.getPassword());
	    System.out.println(System.currentTimeMillis() - b);

	    
	  }
}
